var _currentDataType = "String";
var _currentValueControl = null;
var _attributeTypeMapping = null;
var _currentEntityName = "";
var _currentAttribute = "";
var _currentAttributeCtrl = null;
var _primaryEntityName = "";
var _relatedAttributeName = "";
var DurationOption = "2";
var _isCreateMode = false;
var _readOnlyForm = false;
var _isConditionMode = false;
var _additionalUpdateOperatorList = "String:+=,clear;";
_additionalUpdateOperatorList += "Boolean:clear;";
_additionalUpdateOperatorList += "Picklist:clear;";
_additionalUpdateOperatorList += "PartyList:+=,-=,clear;";
_additionalUpdateOperatorList += "Lookup:clear;";
_additionalUpdateOperatorList += "User:clear;";
_additionalUpdateOperatorList += "Customer:clear;";
_additionalUpdateOperatorList += "Owner:clear;";
_additionalUpdateOperatorList += "Decimal:+=,-=,*=,clear;";
_additionalUpdateOperatorList += "Integer:+=,-=,*=,clear;";
_additionalUpdateOperatorList += "Float:+=,-=,*=,clear;";
_additionalUpdateOperatorList += "Money:+=,-=,*=,clear;DateTime:clear;";
_additionalUpdateOperatorList += "DateTime:clear;";
var _compatibleTypes = new Array();
function GetCompatibleTypeMapping(dataType)
{
if(_compatibleTypes.length == 0)
{
_compatibleTypes["Decimal"] = "Float,Integer,Money";
_compatibleTypes["Float"] = "Decimal,Integer,Money";
_compatibleTypes["Integer"] = "Decimal,Float,Money";
_compatibleTypes["Money"] = "Decimal,Integer,Float";
if (!_isConditionMode)
{
_compatibleTypes["String"] = "Boolean,State,Status,Picklist,PartyList,Lookup,User,Customer,Owner,Decimal,Integer,Float,Money,DateTime,Key";
}
_compatibleTypes["Customer"] = "Lookup,Key";
_compatibleTypes["Lookup"] = "Customer,Owner,Key,User";
_compatibleTypes["Owner"] = "User,Key";
_compatibleTypes["User"] = "Key";
_compatibleTypes["PartyList"] = "Customer,Lookup,Owner,Key";
_compatibleTypes["Key"] = "Customer,Lookup,Owner";
}
return _compatibleTypes[dataType];
}
function GetWorkflowAttributeType(attribute)
{
var dataType = attribute.getAttribute("datatype");
switch(dataType)
{
case "string":
case "nvarchar":
case "text":
case "ntext":
case "memo":
return("String");

case "boolean":
case "bit":
return("Boolean");

case "state":
return("State");

case "status":
return("Status");

case "language":
case "picklist":
return("Picklist");

case "primarykey":
return("Key");

case "partylist":
return("PartyList");

case "uniqueidentifier":
return("UniqueIdentifier");

case "lookup":


var re = new RegExp("(?:^|,)\\s*8\\s*(?:,|$)", "ig");
if(re.test(attribute.lookuptypes))
{
return ("User");
}
return("Lookup");

case "customer":
return("Customer");

case "owner":
return("Owner");

case "decimal":
return("Decimal");

case "int":
case "integer":
return("Integer");

case "number":
case "float":
return("Float");

case "money":
return("Money");

case "datetime":
case "date":
case "time":
return("DateTime");

default:
return dataType;
}
}
function LoadDynamicValuesForAttribute(valueControl, attribute)
{

_currentValueControl = valueControl;
_currentDataType = GetWorkflowAttributeType(attribute);


var entityNameControl = window.document.getElementById('selObjects');

var selectedEntity =  entityNameControl.options[entityNameControl.selectedIndex];
OnEntityChange (selectedEntity.title, selectedEntity.value, attribute);
ShowDurationControl(_currentDataType);



_isConditionMode = true;
}
function OnEntityChangeCore(selectBox, dataSelectBox, entityValue, entityHasDot, attribute)
{
if (IsNull(_currentValueControl))
{
return;
}
if (attribute != undefined)
{
if (!IsNull(attribute.value)&& attribute.value != "" && _isConditionMode)
{

_currentAttribute = attribute.value;
}
else
{

_currentAttribute = attribute.id;
}

_currentAttributeCtrl = attribute;
}
else
{
if(_currentAttributeCtrl != null)
{
attribute = _currentAttributeCtrl;
}
}
var references = null;
if  (!IsNull(attribute))
{
if (!IsNull(attribute.references))
{
references = attribute.references;
}
else if (!IsNull(attribute.lookuptypes))
{
references = attribute.lookuptypes;
}

else if (_currentDataType == "Picklist")
{
var entityName = _currentValueControl.Entity;
if (IsNull(entityName))
{
entityName = _currentEntityName;
}
if (!IsNull(entityName) && entityName.length > 0)
{
var position = entityName.indexOf('.');
if (position != -1)
{
entityName = entityName.substring (0, position);
}
references =  entityName + "." + _currentAttribute;
}
}
}

var index = 0;
for (index = 0; index < dataSelectBox.length; index++)
{
var option = dataSelectBox[index];



dotArray = option.value.split('.');
if ((entityHasDot == false) && (dotArray.length > 2))
{
continue;
}

if (option.value.search(entityValue) != -1)
{
if (!IsNull(references) && references.length > 0)
{

var fAdd = false;
if (option.references != undefined && !IsNull(option.references))
{
var searchInString = "," + option.references + ",";
var referenceTypes = references.split(',');
for (var luIndex = 0; !fAdd && luIndex < referenceTypes.length; luIndex++)
{
if (-1 != searchInString.indexOf("," + referenceTypes[luIndex]+","))
{
fAdd = true;
}
}
}

if (!fAdd)
{
continue;
}
}

addOptionSortedByText(selectBox, option.title, option.value);
}
}
}
function AddCompatibleEntityAttributes(selectBox, entityValue, entityHasDot, attribute, dataType)
{
var compatibleTypes = GetCompatibleTypeMapping(dataType);
if (compatibleTypes != undefined)
{
var typeNames = compatibleTypes.split(',');
var index = 0;
for (index = 0; index < typeNames.length; index++)
{
AddEntityAttributes(selectBox, entityValue, entityHasDot, attribute, typeNames[index]);
}
}
}
function AddEntityAttributes (selectBox, entityValue, entityHasDot, attribute, dataType)
{
var hiddenSelectBox = null;
hiddenSelectBox = window.document.getElementById("valueSelectorHidden" + dataType);


if (IsNull(hiddenSelectBox))
{
return;
}

OnEntityChangeCore(selectBox, hiddenSelectBox, entityValue, entityHasDot, attribute);
}
function OnEntityChange (entityName, entityValue, attribute)
{

var selectBox = window.document.getElementById("valueSelector");
selectBox.options.length = 0;







var entityHasDot = false;
entityArray = entityValue.split('.');
if (entityArray.length > 1)
{
entityHasDot = true;
}


AddEntityAttributes(selectBox, entityValue, entityHasDot, attribute, _currentDataType);

AddCompatibleEntityAttributes(selectBox, entityValue, entityHasDot, attribute, _currentDataType);




if (selectBox.size > 1)
{
selectBox.selectedIndex = -1;
}
else
{
selectBox.selectedIndex = 0;
}
}
function addOptionSortedByText(selectBox, optionText, optionValue)
{

try
{

if(IsOptionFiltered(optionValue))
{
return;
}
}
catch(e)
{
}

var anOption = window.document.createElement("OPTION");

if (selectBox.length == 0)
{
selectBox.options.add(anOption);
}
else
{
var index=0;
for(index=0; index<selectBox.length; index++)
{
if (selectBox[index].innerText > optionText)
{
selectBox.options.add(anOption,index);
break;
}
else if (selectBox[index].value == optionValue)
{
return;
}
}

if (index == selectBox.length)
{
selectBox.options.add(anOption);
}
}

anOption.innerText = optionText;
anOption.value = optionValue;
}
function InsertDataSlug(slugValue, slugText)
{
if (!IsNull(_currentValueControl))
{
var element = _currentValueControl.children(0);
if (typeof(element.InnerControl.IsDataSlug) != undefined)
{

var entityNameControl = window.document.getElementById('selObjects');
if(!IsTimeoutCondition(_currentValueControl.ParentControl) )
{
slugText = entityNameControl.options[entityNameControl.selectedIndex].text + '-' + slugText;
}

InsertDataSlugCore(element.InnerControl, slugValue, slugText, false, null, null, null, true, false);
element.Text = slugText;
element.DefaultText = slugText;

}
}
else
{

}
}
function UpdateOperator(control, value, text)
{
if ((control.IsDataSlug) && !IsNull(value) && !IsNull(text))
{
control.UpdateOperatorControl(value, text,getOperatorTextForValue(text));
}
}
function InsertDataSlugCore(control, value, text, isMultiSlug, attributeName, operatorValue, operatorText, isReadOnly, isDefaultValue)
{
control.IsDisabled = _readOnlyForm;
control.IsDataSlug = true;
control.IsMultiSlug = isMultiSlug;
control.IsReadOnly = isReadOnly;

if (!IsNull(attributeName))
{
control.AttributeName = attributeName;
}
if(IsEmailBody(control))
{
control.UpdateDataField(value, text, isReadOnly);
}
else
{
control.InsertSlugControl(value,text,isDefaultValue);
UpdateOperator(control,operatorValue,operatorText);
if(_currentDataType == "DateTime")
{
var beforeAfter = window.document.getElementById("beforeAfterSelector");
if(!IsNull(beforeAfter) && beforeAfter.value != DurationOption)
{
ResetDurationValue();
}
}
}
}
function UpdateControlInnerHtml(control, htmlString)
{

control.IsDisabled = _readOnlyForm;
control.IsDataSlug = true;
control.UpdateInnerHtml(htmlString);
}



function RemoveFromDynamicValueList()
{
var listControl = window.document.getElementById("dynamicValueSelector");
if (listControl.selectedIndex >= 0)
{
listControl.remove(listControl.selectedIndex);
}

}


function AddToDynamicValueList()
{
var selectBox = window.document.getElementById("valueSelector");


if (selectBox.selectedIndex == -1)
{
return;
}

var selectedOption = selectBox[selectBox.selectedIndex];


if (IsNull(selectedOption))
{
return;
}

var listControl = window.document.getElementById("dynamicValueSelector");


var index = 0;
for (index=0; index < listControl.length; index++)
{
if (listControl[index].value == selectedOption.value)
{
break;
}
}



if (index == listControl.length)
{
var anOption = window.document.createElement("OPTION");
listControl.options.add(anOption);



var entityNameControl = window.document.getElementById('selObjects');
anOption.innerText = selectedOption.innerText + "(" + entityNameControl.options[entityNameControl.selectedIndex].text + ")";
anOption.value = selectedOption.value;
}


if (listControl.length > 0)
{
EnableOkButton();
}
}
function EnableOkButton()
{
EnableDisableOkButton(false);
}
function DisableOkButton()
{
EnableDisableOkButton(true);
}
function EnableDisableOkButton(disable)
{
var buttonCell = window.document.getElementById("wfDynamicExpressionOk");
if (!IsNull(buttonCell))
{
buttonCell.disabled = disable;
}
}
function setOperatorAsClearOperator()
{
var operatorControl = window.document.getElementById("operatorSelector");
var index = 0;
for (index=0; index<operatorControl.options.length; index++)
{
if (operatorControl.options[index].value == "clear")
{
operatorControl.selectedIndex = index;
OperatorChange();
break;
}
}
}
function EnableOkAddButtons()
{
EnableDisableOkAddButtons(false);
}
function DisableOkAddButtons()
{
EnableDisableOkAddButtons(true);
}
function EnableDisableOkAddButtons(disable)
{
var addButton = window.document.getElementById("wfDynamicExpressionAdd");
var okButton = window.document.getElementById('wfDynamicExpressionOk');
var monthControl = window.document.getElementById('monthSelector');
var dayControl = window.document.getElementById('daySelector');
var hourControl = window.document.getElementById('hourSelector');
var minuteControl = window.document.getElementById('minuteSelector');
var beforeAfterControl = window.document.getElementById('beforeAfterSelector');
var entitySelectorControl = window.document.getElementById('selObjects');
var attributeSelectorControl = window.document.getElementById('valueSelector');
var dynamicValueControl = window.document.getElementById('dynamicValueSelector');
var defaultValueSpan = window.document.getElementById('defaultValueControlSpan');

addButton.disabled = disable;
okButton.disabled = disable;
entitySelectorControl.disabled = disable;
attributeSelectorControl.disabled = disable;
dynamicValueControl.disabled = disable;
defaultValueSpan.disabled = disable;

if (!IsNull(monthControl))
{
monthControl.disabled = disable;
dayControl.disabled = disable;
hourControl.disabled = disable;
minuteControl.disabled = disable;
beforeAfterControl.disabled = disable;
}
}
function OperatorChange()
{

var operator = GetOperatorForSlug();





if (operator[1] == "clear")
{

DisableOkAddButtons();


_currentValueControl.value = "";


var expression = GetExpressionForClearValueSlug();
var attributeName = GetEntityAttributeName(_currentValueControl);
var isMultiSlug = false;
var isReadOnly = true;

InsertDataSlugCore(_currentValueControl, expression[0], expression[1], isMultiSlug, attributeName, operator[0], operator[1], isReadOnly, false);
}
else
{

EnableOkAddButtons();


if ((_currentValueControl.IsDataSlug != undefined) && (_currentValueControl.IsDataSlug == true))
{
if (_currentValueControl.CurrentOperatorIsClear())
{
_currentValueControl.DeleteDataSlug();
}
}


if ((_currentValueControl.IsDataSlug != undefined) &&
(_currentValueControl.IsDataSlug == true))
{
UpdateOperator(_currentValueControl,operator[0],operator[1]);
}
}


if ((operator[1] == "clear") || (operator[1] == "="))
{
SetIgnoreRangeProperty(false);
}
else
{
SetIgnoreRangeProperty(true);
}
}
function SetIgnoreRangeProperty(ignoreRange)
{
switch (_currentDataType)
{
case "Decimal":
case "Integer":
case "Float":
case "Money":

var defaultValueControl = window.document.getElementById("defaultValueControlSpan");


var inputControls = defaultValueControl.getElementsByTagName("INPUT");

for (var index = 0; index < inputControls.length; index++)
{
if (inputControls[index].IgnoreRange != undefined)
{
inputControls[index].IgnoreRange = ignoreRange;


inputControls[index].value = "";
}
}

break;

default:
break;
}
}

function MoveDynamicValueUp()
{
var listControl = window.document.getElementById("dynamicValueSelector");


if ((listControl.length > 0) && (listControl.selectedIndex > 0))
{
var currentOption = listControl.options[listControl.selectedIndex];
var oneAboveOption = listControl.options[listControl.selectedIndex - 1];
var tempValue = oneAboveOption.value;
var tempText = oneAboveOption.innerText;

oneAboveOption.innerText = currentOption.innerText;
oneAboveOption.value = currentOption.value;

currentOption.innerText = tempText;
currentOption.value = tempValue;

listControl.selectedIndex--;
}
}

function MoveDynamicValueDown()
{
var listControl = window.document.getElementById("dynamicValueSelector");


if ((listControl.length > 0) && (listControl.selectedIndex >= 0) && (listControl.selectedIndex < (listControl.length - 1)))
{
var currentOption = listControl.options[listControl.selectedIndex];
var oneBelowOption = listControl.options[listControl.selectedIndex + 1];
var tempValue = oneBelowOption.value;
var tempText = oneBelowOption.innerText;

oneBelowOption.innerText = currentOption.innerText;
oneBelowOption.value = currentOption.value;

currentOption.innerText = tempText;
currentOption.value = tempValue;

listControl.selectedIndex++;
}


}
function GetValueForTag(tag)
{
var startIndex = window.location.search.indexOf(tag);

if (startIndex == -1)
{
return null;
}

var length = tag.length;


var subString = window.location.search.substr(startIndex + length);


var endIndex = subString.indexOf("&");
if (endIndex == -1)
{
return subString;
}
else
{
return subString.substr(0, endIndex);
}
}
function SetReadOnlyForm(val)
{
_readOnlyForm = val;
}
function  GenerateCustomActivityTypeMapping (workflowId, activityName)
{
_isCreateMode = true;
var command = new RemoteCommand("Workflow", "GetCustomActivityDataTypeInfo");
command.SetParameter("workflowId", workflowId);
command.SetParameter("activityName", activityName);
var oResult = command.Execute();
if (oResult.Success)
{
SetAttributeTypeMapping(_currentEntityName, oResult.ReturnValue);
}
}
function GenerateAttributeTypeMapping (entityName, primaryEntityName, relatedAttributeName, isCreateMode)
{

_isCreateMode = isCreateMode;

if (entityName == _currentEntityName)
{

return;
}
_currentEntityName = entityName;

if (!IsNull(primaryEntityName))
{
_primaryEntityName = primaryEntityName;
}

if (!IsNull(relatedAttributeName))
{
_relatedAttributeName = relatedAttributeName;
}

var command = new RemoteCommand("Workflow", "GetAttributeDataTypeInfo");
command.SetParameter("entityName", entityName);
var oResult = command.Execute();

if (oResult.Success)
{
SetAttributeTypeMapping(_currentEntityName, oResult.ReturnValue);
}
else
{
SetAttributeTypeMapping(_currentEntityName, null);
}
}
function AddAttributeTypeMapping(attributeName, attributeType)
{
_attributeTypeMapping += "&"+attributeName+";"+attributeType;
}
function SetAttributeTypeMapping(entityName, attributeMapping)
{
if(!IsNull(attributeMapping))
{
_attributeTypeMapping = attributeMapping;
_currentEntityName = entityName;


var attrList = _attributeTypeMapping.split("&");
var index = 0;
for (index = 0; index < attrList.length; index++)
{
var attrWithData = attrList[index];
var lst = attrWithData.split(";");
var attrName = lst[0];


var attrControl = window.document.getElementById(attrName);

if (IsNull(attrControl))
{
attrControl = window.document.getElementById(attrName+"Select");
}
else if (!IsNull(attrControl) && (attrControl.className == "ms-crm-Duration"))
{



attrControl = window.document.getElementById(attrName+"Select");
}

if(!IsNull(attrControl) && attrControl.className == "ms-crm-Email-Body")
{

attrControl.attachEvent("onbodyfocus", OnAttributeFocus);
}
else if(!IsNull(attrControl) && attrControl.className == "ms-crm-SelectBox")
{
attrControl.attachEvent("oncontrolfocus", OnAttributeFocus);
}
else if (!IsNull(attrControl) && (attrControl.contentEditable != "false"))
{
attrControl.attachEvent("onfocus", OnAttributeFocus);
}
}
}
else
{
_attributeTypeMapping = "";
}
}
function GetDataTypeForAttribute(attribute)
{

if (IsNull(attribute) || (attribute == ""))
{
return null;
}







var startIndex = _attributeTypeMapping.indexOf(attribute + ";");
var length = attribute.length + 1;

if (startIndex != 0)
{



startIndex = _attributeTypeMapping.indexOf("&" + attribute + ";");
length = attribute.length + 2;
}

if (startIndex < 0)
{

return null;
}


var subString = _attributeTypeMapping.substr(startIndex + length);


var endIndex = subString.indexOf("&");

if (endIndex != -1)
{
return subString.substr(0, endIndex);
}

return subString;
}
function OnAttributeFocus()
{
SetDatatypeAttributeListAtFocus(event.srcElement);
}
function SetDatatypeAttributeListAtFocus(srcElement)
{

var entityNameControl = window.document.getElementById('selObjects');
if (IsNull(entityNameControl) || _readOnlyForm)
{
return;
}

var previousDataType = _currentDataType;
var previousDataControl = _currentValueControl;
var control = srcElement;
var controlId = control.id;
var durationInputControl = null;

var controlIdForDataType = controlId;
if (control.className == "ms-crm-SelectBox")
{
controlIdForDataType = controlId.substring(0,controlId.indexOf("Select"));
durationInputControl = window.document.getElementById(controlIdForDataType);
}

var dataType = GetDataTypeForAttribute (controlIdForDataType);

if (!IsNull(dataType))
{
_currentDataType = dataType;

AddDefaultOperator();


if (!_isCreateMode)
{
AddOperatorForDataType (_additionalUpdateOperatorList, dataType);
}


var durationControl = window.document.getElementById("durationControl");
if (!IsNull(durationControl))
{
if (_currentDataType == "DateTime")
{
durationControl.style.display = 'block';
}
else
{
durationControl.style.display = 'none';
}
}


var selectedEntity =  entityNameControl.options[entityNameControl.selectedIndex];



var listControl = window.document.getElementById("dynamicValueSelector");
listControl.length = 0;

_currentValueControl = GetValueControlWithSlug(window.document.getElementById(controlId));

if(previousDataControl ==_currentValueControl)
{
if (_currentValueControl.CurrentOperatorIsClear())
{
DisableOkButton();
}
else
{
EnableOkAddButtons();
}
return;
}
OnEntityChange (selectedEntity.title, selectedEntity.value, control);


var defaultValueControl = window.document.getElementById("defaultValueControlSpan");
switch(_currentDataType)
{
case "String":
if(_currentValueControl.className == "ms-crm-Email-Body")
{
defaultValueControl.innerHTML = "<INPUT class=ms-crm-Text id=" + _currentValueControl.id + " style=\"IME-MODE: auto\" tabIndex=1040 maxLength=200 req=\"0\">";
}
else
{
defaultValueControl.innerHTML =  _currentValueControl.outerHTML;
}
break;

case "Money":
var currentControl = _currentValueControl;
while (currentControl != document.documentElement)
{
if ((currentControl.className == "ms-crm-Money") && (currentControl.tagName == "TABLE"))
{
break;
}

currentControl = currentControl.parentElement;
}

if (currentControl == document.documentElement)
{
defaultValueControl.innerHTML =  _currentValueControl.outerHTML;
}
else
{
defaultValueControl.innerHTML =  currentControl.outerHTML;
}
break;

default:
defaultValueControl.innerHTML =  _currentValueControl.outerHTML;
break;
}


defaultValueControl.children[0].style.display = 'inline';


defaultValueControl.children[0].id += "DefaultValueControl";
switch (_currentDataType)
{
case "DateTime":
case "Integer":

var tablesToRemove = 1;



if ((_currentDataType == "Integer") && (control.className == "ms-crm-SelectBox") && (durationInputControl != null))
{
defaultValueControl.innerHTML = durationInputControl.outerHTML + defaultValueControl.innerHTML;
defaultValueControl.children[0].id += "DefaultValueControl";


tablesToRemove = 2;
}

if ((_currentDataType == "DateTime") || ((_currentDataType == "Integer") && (control.className == "ms-crm-SelectBox")))
{
var tables = defaultValueControl.getElementsByTagName('TABLE')

var tablesRemoved = 0;
for (var tableIndex = tables.length; tableIndex > 0; tableIndex --)
{
var tableElement = tables[tableIndex-1];
if(tableElement.id == 'selectTable')
{
tableElement.parentElement.removeChild(tableElement);
tableElement.style.visibility='hidden';

tablesRemoved++;

if (tablesRemoved == tablesToRemove)
{
break;
}
}
}
}

break;
case "Boolean":

var inputControls = defaultValueControl.children[0].getElementsByTagName("INPUT");
var inputLabels = defaultValueControl.children[0].getElementsByTagName("LABEL");
for (var index = 0; index < inputControls.length; index++)
{
if (inputControls[index].className == "ms-crm-RadioButton")
{
inputControls[index].id = "rad_" + defaultValueControl.children[0].id + (index+1);
inputLabels[index].attributes["for"].value = inputControls[index].id;
}
}
break;
case "Money":

var inputControls = defaultValueControl.children[0].getElementsByTagName("INPUT");
var index=0;
for (index = 0; index < inputControls.length; index++)
{
if (inputControls[index].className == "ms-crm-Money")
{
inputControls[index].id += "DefaultValueControl";



inputControls[index].IgnoreCurrencySymbol = true;
}
}
break;
case "Lookup":
case "User":
case "Owner":
case "Customer":
case "PartyList":

var lookupImgs = defaultValueControl.getElementsByTagName("IMG");
if (lookupImgs.length > 0)
{
lookupImgs[0].id = lookupImgs[0].id + "DefaultValueControl";
}

var currentControl = _currentValueControl.parentElement;
while (currentControl != document.documentElement)
{
if (!IsNull(currentControl.id) && currentControl.id.substr(currentControl.id.length - 2) == "_d")
{
break;
}
currentControl = currentControl.parentElement;
}
var newParentId = currentControl.id.slice(0, -2) + "DefaultValueControl_d";


defaultValueControl.innerHTML = "<table cellspacing='0' cellpadding='0'><tbody><tr><td id=" +
newParentId + ">" + defaultValueControl.innerHTML + "</td></tr></tbody></table>";
break;
case "Picklist":
defaultValueControl.firstChild.attributes.removeNamedItem('name');
break;
}


ResetTabIndexRecursive(defaultValueControl);

ResetDefaultValue(defaultValueControl);


EnableOkAddButtons();



if ((_currentValueControl.IsDataSlug != undefined) && (_currentValueControl.IsDataSlug == true))
{
if (_currentValueControl.CurrentOperatorIsClear())
{
setOperatorAsClearOperator();
DisableOkButton();
}
}

}
}
function ResetTabIndexRecursive(control)
{
control.tabIndex = -1;
for(var child = 0; child < control.children.length; child ++)
{
ResetTabIndexRecursive(control.children(child));
}
}
function GetValueControlWithSlug(ctrl)
{
var currentCtrl = ctrl;
while(ctrl != null && ctrl.IsDataSlug == undefined)
{
ctrl = ctrl.parentElement;
}
if (ctrl == null)
{
return currentCtrl;
}
return ctrl;
}
function AddDefaultOperator()
{
var operatorControl = window.document.getElementById("operatorSelector");
operatorControl.options.length = 0;


AddOperator(operatorControl, "=");


operatorControl.disabled = true;
}
function AddOperator(operatorControl, optionValue)
{
var anOption = window.document.createElement("OPTION");
operatorControl.options.add(anOption);
anOption.value = optionValue;
anOption.innerText = getOperatorTextForValue(optionValue);
}
function getOperatorTextForValue(operatorValue)
{
if (operatorValue == "=")
{
return LOCID_DE_OPERATOR_EQUAL;
}

if (operatorValue == "+=")
{
return LOCID_DE_OPERATOR_INCREMENTBY;
}

if (operatorValue == "-=")
{
return LOCID_DE_OPERATOR_DECREMENTBY;
}

if (operatorValue == "*=")
{
return LOCID_DE_OPERATOR_MULTIPLYBY;
}

if (operatorValue == "clear")
{
return LOCID_DE_OPERATOR_CLEAR;
}
}

function AddOperatorForDataType(additionalOperators, dataType)
{
var operatorControl = window.document.getElementById("operatorSelector");


var startIndex = additionalOperators.indexOf(dataType + ":");

if (startIndex < 0)
{

operatorControl.disabled = true;
return;
}
else
{

operatorControl.disabled = false;
}

var length = dataType.length + 1;

var subString = additionalOperators.substr(startIndex + length);
var endIndex = subString.indexOf(";");

var operators = subString.substr(0,endIndex);


var operatorList = operators.split(",");
var index = 0;
for (index = 0; index < operatorList.length; index++)
{
var operator = operatorList[index];
AddOperator(operatorControl, operator);
}
}
function InsertSlug()
{
if (!IsNull(_currentValueControl) && (_currentValueControl.IsDataSlug != undefined))
{

var expression = GetExpressionAndDefaultValueForSlug();
if (expression[0] == undefined)
{
return;
}


var operator = GetOperatorForSlug();
var attributeName = GetEntityAttributeName(_currentValueControl);
var isMultiSlug = IsMultiSlug(_currentDataType,_currentValueControl);
var isReadOnly = IsReadOnly(_currentDataType);

InsertDataSlugCore(_currentValueControl, expression[0], expression[1], isMultiSlug, attributeName, operator[0], operator[1], isReadOnly, false);
}
}
function GetEntityAttributeName(control)
{
if (control.InnerControlId != undefined)
{
return control.InnerControlId;
}

if (control.className == "ms-crm-SelectBox")
{
return control.id.substring(0,control.id.indexOf("Select"));
}

return control.id;
}
function GetSelfPropertyExpression (operator)
{

if ((operator == "=") || (operator == "clear"))
{
return null;
}
var entityNameValue = GetFullEntityName();

if(entityNameValue==null)
{
entityNameValue = _currentEntityName;
if (!IsNull(_relatedAttributeName) && _relatedAttributeName!="")
{
entityNameValue += "." + _relatedAttributeName;
}
}


var attributeName = GetEntityAttributeName(_currentValueControl);
var expression = "<slugelement type=\"slug\">" +
"<slug type=\"dynamic\" value=\"" +
entityNameValue + "." +  attributeName +
"\"/>" +
"</slugelement>";

return expression;
}
function GetFullEntityName()
{
var currentEntityKey = document.getElementById("hidCurrentEntityPath");
if(!IsNull(currentEntityKey))
{
return currentEntityKey.value;
}
return null;
}
function GetUpdatedOperator(operator)
{
var returnValue = new Array();


returnValue[1] = operator;

var operatorValue = "=";

if (operator == "=")
{
operatorValue = "=";
}

if (operator == "+=")
{
operatorValue = "+";
}

if (operator == "-=")
{
operatorValue = "-";
}

if (operator == "*=")
{
operatorValue = "*";
}

if (operator == "/=")
{
operatorValue = "/";
}

if (operator == "clear")
{
operatorValue = "clear";
}

var selfPropertyExpression = GetSelfPropertyExpression(operatorValue);

returnValue[0] = "<slugelement type=\"operator\" value=\"" + operatorValue + "\"/>";

if (!IsNull(selfPropertyExpression))
{
returnValue[0] += selfPropertyExpression;
}

return returnValue;
}



function GetExpressionForClearValueSlug()
{
var returnValue = new Array();
returnValue[0] = "<slugelement type=\"slug\">" + "<slug type=\"dynamic\" value=\"" + "null" + "\"/>" + "</slugelement>";
returnValue[1] = "{" + LOCID_DE_OPERATOR_CLEAR + "}";

return returnValue;
}
function GetExpressionAndDefaultValueForSlug()
{
var returnValue = new Array();


var expressionValue = GetExpressionForSlug();


var defaultValue = GetDefaultValueForSlug();

if ((expressionValue[0] != undefined) || (defaultValue[0] != undefined))
{
returnValue[0] = "<slugelement type=\"slug\">";
returnValue[1] = "{";

if (expressionValue[0] != undefined)
{
returnValue[0] += expressionValue[0];
returnValue[1] += expressionValue[1];
}

if (defaultValue[0] != undefined)
{
returnValue[0] += defaultValue[0];
if(returnValue[1] == "{")
{
returnValue[1] += defaultValue[1];
}
else
{
returnValue[1] +=";" + defaultValue[1];
}

}

returnValue[0] += "</slugelement>";
returnValue[1] += "}";
}

return returnValue;
}
function GetExpressionForSlug()
{
var returnValue = new Array();
var listControl = window.document.getElementById("dynamicValueSelector");


if (listControl.length <= 0)
{
return returnValue;
}


var durationExpression = GetDurationExpression();

var index = 0;
for (index = 0; index < listControl.length; index++)
{
var option = listControl.options[index];

if (index == 0)
{
returnValue[1] = option.innerText;
returnValue[0] = "<slug type=\"dynamic\" value=\"" + option.value + "\"/>";
}
else
{
returnValue[1] += ";" + option.innerText;
returnValue[0] += "<slug type=\"dynamic\" value=\"" + option.value + "\"/>";
}
}

if (durationExpression[0] != undefined)
{
returnValue[0] = durationExpression[0] + returnValue[0];
returnValue[1] = durationExpression[1] + returnValue[1];
}

return returnValue;
}
function GetDataXmlForControl(control)
{
if (control.DataXml != undefined)
{
return control.DataXml;
}

if (control.InnerDataXml != undefined)
{
return control.InnerDataXml;
}

return "";
}

function GetFirstChildWithDataValue(control)
{
if (control.DataValue == undefined)
{
if (control.hasChildNodes())
{
return GetFirstChildWithDataValue(control.firstChild);
}
else
{
return null;
}
}
return control;
}
function GetDefaultValueForSlug()
{
var returnValue = new Array();
var defaultValueControl = window.document.getElementById("defaultValueControlSpan").children[0];
if (defaultValueControl.tagName=="TABLE" && defaultValueControl.DataValue == undefined && defaultValueControl.hasChildNodes())
{

var childLookupValueControl = GetFirstChildWithDataValue(defaultValueControl);
if (childLookupValueControl != null)
{
defaultValueControl = childLookupValueControl;
}
}

if (!IsNull(defaultValueControl.DataValue))
{
returnValue[0] = "<slug type=\"defaultvalue\">" + GetDataXmlForControl(defaultValueControl) + "</slug>";



if (_currentDataType == "Boolean")
{
var inputControls = defaultValueControl.children[0].getElementsByTagName("INPUT");
var inputLabels = defaultValueControl.children[0].getElementsByTagName("LABEL");

var valueToMatch = "0";
if (defaultValueControl.DataValue == true)
{
valueToMatch = "1";
}

for (var index = 0; index < inputControls.length; index++)
{
if ((inputControls[index].className == "ms-crm-RadioButton") &&
(inputControls[index].value == valueToMatch))
{
returnValue[1] = inputLabels[index].innerText;
break;
}
}
}
else if (_currentDataType == "Picklist")
{
if (defaultValueControl.options[defaultValueControl.selectedIndex].innerText != "")
{
returnValue[1] = defaultValueControl.options[defaultValueControl.selectedIndex].innerText;
}
else
{
returnValue[1] = "";
}
}
else if (_currentDataType == "DateTime")
{

returnValue[1] = FormatDate(defaultValueControl.DataValue)+ " " + timeToString(defaultValueControl.DataValue);
}
else if (_currentDataType == "PartyList")
{
var innerParentControl = defaultValueControl.firstChild.firstChild.firstChild.firstChild;
var index = 0;
for (index=0; index < innerParentControl.children.length; index++)
{
if (index == 0)
{
returnValue[1] = innerParentControl.children[index].innerText;
}
else
{
returnValue[1] += ", " + innerParentControl.children[index].innerText;
}
}
}
else if ((_currentDataType == "Integer") && (defaultValueControl.className == "ms-crm-Duration"))
{

var controlId = defaultValueControl.id;
var sanitizedControlId = controlId.substring(0,controlId.indexOf("DefaultValueControl")) + "Select" + "DefaultValueControl";
var selectControl = window.document.getElementById(sanitizedControlId);
if (!IsNull(selectControl))
{
returnValue[1] = selectControl.returnValue;
}
}
else
{
if (defaultValueControl.innerText != "")
{
returnValue[1] =  defaultValueControl.innerText;
}
else
{
returnValue[1] =  defaultValueControl.value;
}
}
}

return returnValue;
}
function ResetDefaultValue(defaultValueControl)
{
defaultValueControl = defaultValueControl.children[0];

if (defaultValueControl.tagName=="TABLE" && defaultValueControl.DataValue == undefined && defaultValueControl.hasChildNodes())
{

var childLookupValueControl = GetFirstChildWithDataValue(defaultValueControl);
if (childLookupValueControl != null)
{
defaultValueControl = childLookupValueControl;
}
}

if (!IsNull(defaultValueControl.DataValue))
{
defaultValueControl.DataValue = null;
}
else
{
if (defaultValueControl.className=="ms-crm-DateTime")
{
defaultValueControl.rows[0].cells[0].children[0]
var dateControl = defaultValueControl.rows[0].cells[0].children[0];
if(!IsNull(dateControl) && dateControl.id == "DateInput")
{
dateControl.value = "";
dateControl.returnValue = "";
}
}
}
}
function GetDurationExpression()
{
var returnValue = new Array();


if (_currentDataType != "DateTime")
{
return returnValue;
}


var month = GetSelectedOptionForControlId("monthSelector");
var day = GetSelectedOptionForControlId("daySelector");
var hour = GetSelectedOptionForControlId("hourSelector");
var minute = GetSelectedOptionForControlId("minuteSelector");
var beforeAfter = GetSelectedOptionForControlId("beforeAfterSelector");


if ((month.title == "0") && (day.title == "0") && (hour.title == "0") && (minute.title == "0"))
{
return returnValue;
}

returnValue[0] = "<slug type=\"duration\" value=\"" +
month.title + ";" +
day.title + ";" +
hour.title + ";" +
minute.title + ";" +
beforeAfter.value +
"\"/>";

returnValue[1] = null;

if (month.title != "0")
{
returnValue[1] = month.desc;
}

if (day.title != "0")
{
if (IsNull(returnValue[1]))
{
returnValue[1] = day.desc;
}
else
{
returnValue[1] += ", " + day.desc;
}
}

if (hour.title != "0")
{
if (IsNull(returnValue[1]))
{
returnValue[1] = hour.desc;
}
else
{
returnValue[1] += ", " + hour.desc;
}
}

if (minute.title != "0")
{
if (IsNull(returnValue[1]))
{
returnValue[1] = minute.desc;
}
else
{
returnValue[1] += ", " + minute.desc;
}
}

returnValue[1] += " " + beforeAfter.title + " ";

return returnValue;
}
function GetSelectedOptionForControlId(id)
{
var control = window.document.getElementById(id);

return control.options[control.selectedIndex];
}
function GetOperatorForSlug()
{
var returnValue = new Array();

var operatorControl = window.document.getElementById("operatorSelector");
var operator = operatorControl.options[operatorControl.selectedIndex].value;
returnValue = GetUpdatedOperator(operator);

return returnValue;
}
function PopulateSlugControls(slugInfo)
{
PopulateSlugControlsWithCallback(slugInfo, null, null);
}
function PopulateSlugControlsWithCallback(slugInfo, eventName, eventHandler)
{

var slugInfoList = slugInfo.split("<;>");

var index = 0;
for (index = 0; index < slugInfoList.length; index++)
{
var info = slugInfoList[index];
var infoList = info.split("<:>");


PopulateControl(infoList[0], infoList[1], eventName, eventHandler);
}
}
function PopulateControl(controlId, valueXml, eventName, eventHandler)
{
var control = GetValueControlWithSlug(window.document.getElementById(controlId));
var dataType = GetDataTypeForAttribute (controlId);

var isMultiSlug = IsMultiSlug(dataType,control);
var isReadOnly = IsReadOnly(dataType);



if (!IsNull(control) && (control.className == "ms-crm-Duration"))
{
control = window.document.getElementById(control.id+"Select");
}

if (!IsNull(control) && (control.IsDataSlug != undefined))
{

if (!(IsNull(eventName)||IsNull(eventHandler)))
{
control.attachEvent(eventName, eventHandler);
}
var parsedInfo = ParseOperatorAndSlugInfo(valueXml);


var dataXml = "<data>" + parsedInfo[2] + "</data>";
var xmlDoc = loadXmlDocument(dataXml);
if(IsEmailBody(control))
{
control.IsMultiSlug = true;
control.IsReadOnly = isReadOnly;
control.IsDataSlug = true;
control.AttributeName = controlId;
PopulateEmailBody(control, xmlDoc,dataType);
return ;
}

var index=0;
for(index=0; index < xmlDoc.firstChild.childNodes.length; index++)
{
var childNode = xmlDoc.firstChild.childNodes[index];

var slugInfo = GetSlugInfo(childNode,dataType);

if (IsNull(slugInfo[0]))
{

UpdateControlInnerHtml(control, CrmEncodeDecode.CrmXmlDecode(slugInfo[1]));
}
else
{

if (parsedInfo[1] == "clear")
{
isMultiSlug = false;
isReadOnly = true;
}

InsertDataSlugCore(control, slugInfo[0], slugInfo[1], isMultiSlug, controlId, parsedInfo[0], parsedInfo[1], isReadOnly, true);
}
}
}
}

function PopulateEmailBody(control, xmlDoc,dataType)
{
var index=0;
var html = "";
control.IsDataSlug = true;
for(index=0; index < xmlDoc.firstChild.childNodes.length; index++)
{
var childNode = xmlDoc.firstChild.childNodes[index];

var slugInfo = GetSlugInfo(childNode,dataType);
if (IsNull(slugInfo[0]))
{
html += CrmEncodeDecode.CrmXmlDecode(slugInfo[1]);
}
else
{
html += "<SPAN class=\"ms-crm-DataSlug\" contentEditable=\"false\" style=\"DISPLAY: inline\" tabIndex=\"-1\" value='";
html += slugInfo[0] + "'>";
html += CrmEncodeDecode.CrmXmlDecode(slugInfo[1]) ;
html += "</SPAN> " ;
}
}

if(IsNull(control.InsertValue(html)))
{

var emailBody = document.getElementById("hidEmailBodyContent");
emailBody.value = html;
}
}
function GetSlugInfo(xmlNode,dataType)
{
var returnValue = new Array();
var attrType = xmlNode.attributes[0];

if ((attrType.nodeName == "type") && (attrType.nodeValue == "primitive"))
{

returnValue[0] = null;




var startIndex = xmlNode.attributes[1].xml.indexOf("\"") + "\"".length;
var endIndex = xmlNode.attributes[1].xml.indexOf("\"", startIndex);
returnValue[1] = xmlNode.attributes[1].xml.substr(startIndex, endIndex-startIndex);
}
else if ((attrType.nodeName == "type") && (attrType.nodeValue == "slug"))
{
returnValue[0] = xmlNode.xml;
returnValue[1] = GetTextForNode(xmlNode,dataType);
}

return returnValue;
}
function ParseOperatorAndSlugInfo(valueXml)
{
var dataXml = "<data>" + valueXml + "</data>";
var xmlDoc = loadXmlDocument(dataXml);

var operatorText = null;
var operatorValue = null;
var slugValue = null;

var slugBody = xmlDoc.firstChild;

var index = 0;
var childNode;
for (index = 0; index < slugBody.childNodes.length; index++)
{
childNode = slugBody.childNodes[index];


var attrType = childNode.attributes[0];

if ((attrType.nodeName == "type") && (attrType.nodeValue == "operator"))
{

operatorText = childNode.attributes[1].nodeValue;
operatorValue = childNode.xml;
break;
}
}


if ((operatorText != "=") && (operatorText != "clear"))
{
operatorText += "=";
operatorValue += slugBody.childNodes[++index].xml;
}

var newIndex = 0;
for (newIndex = index+1; newIndex < slugBody.childNodes.length; newIndex++)
{
childNode = slugBody.childNodes[newIndex];

if (IsNull(slugValue))
{
slugValue = childNode.xml;
}
else
{
slugValue += childNode.xml;
}
}

var returnValue = new Array();
returnValue[0] = operatorValue;
returnValue[1] = operatorText;
returnValue[2] = slugValue;

return returnValue;
}
function GetTextForNode(xmlNode, dataType)
{
var outputText = null;

var attrType = xmlNode.attributes[0];

if (!((attrType.nodeName == "type") && (attrType.nodeValue == "slug")))
{
return null;
}

var index = 0;
var durationText = null;
for (index = 0; index < xmlNode.childNodes.length; index++)
{
var childNode = xmlNode.childNodes[index];

var nodeType = childNode.attributes[0];
if (nodeType.nodeName == "type" && nodeType.nodeValue == "dynamic")
{
var displayValue = GetSlugDisplayText(childNode.attributes[1].nodeValue, dataType);

if (IsNull(displayValue))
{
continue;
}

if (IsNull(outputText))
{
outputText = displayValue;
}
else
{
outputText += ";" + displayValue;
}
}
else if (nodeType.nodeName == "type" && nodeType.nodeValue == "defaultvalue")
{
var defaultDisplayValue = GetDefaultValueDisplayText(childNode.firstChild, dataType);

if (!IsNull(defaultDisplayValue))
{
if (IsNull(outputText))
{
outputText = defaultDisplayValue;
}
else
{
outputText += ";" + defaultDisplayValue;
}
}
}
else if (nodeType.nodeName == "type" && nodeType.nodeValue == "duration")
{
durationText = GetDurationDisplayText(childNode.attributes[1].nodeValue);
}
}

if (!IsNull(durationText))
{
outputText = durationText + outputText;
}

return "{" + outputText + "}";
}
function GetDurationDisplayText(value)
{
var list = value.split(";");
var month = GetOptionValueWithTitleForDuration("monthSelector",list[0]);
var day = GetOptionValueWithTitleForDuration("daySelector",list[1]);
var hour = GetOptionValueWithTitleForDuration("hourSelector",list[2]);
var minute = GetOptionValueWithTitleForDuration("minuteSelector",list[3]);
var beforeAfter = GetOptionTitleWithValue("beforeAfterSelector",list[4]);

var displayText = null;
if (!IsNull(month))
{
displayText = month;
}

if (!IsNull(day))
{
if (IsNull(displayText))
{
displayText = day;
}
else
{
displayText += ", " + day;
}
}

if (!IsNull(hour))
{
if (IsNull(displayText))
{
displayText = hour;
}
else
{
displayText += ", " + hour;
}
}

if (!IsNull(minute))
{
if (IsNull(displayText))
{
displayText = minute;
}
else
{
displayText += ", " + minute;
}
}

displayText += " " + beforeAfter + " ";

return displayText;
}
function GetOptionValueWithTitleForDuration(controlId,title)
{
if (title == "0")
{
return null;
}

return GetOptionValueWithTitle(controlId,title);
}
function GetSlugDisplayText(value, dataType)
{

if (value == "null")
{
return LOCID_DE_OPERATOR_CLEAR;
}
var attributeName = null;
attributeName = GetOptionTitleWithValue("valueSelectorHidden" + dataType, value);



if (IsNull(attributeName))
{
attributeName = RetryMatchDataType(dataType,value);
}
if (IsNull(attributeName))
{

var compatibleTypes = GetCompatibleTypeMapping(dataType);
if (compatibleTypes != undefined)
{
var typeNames = compatibleTypes.split(',');
var index = 0;
for (index = 0; index < typeNames.length; index++)
{
attributeName = GetOptionTitleWithValue("valueSelectorHidden" + typeNames[index], value);
if (IsNull(attributeName))
{
attributeName = RetryMatchDataType(typeNames[index],value);

}

if (!IsNull(attributeName))
{
dataType = typeNames[index];
break;
}
}
}
}




var list = value.split(".");
var entityValue = null;
for (index = 0; index < list.length-1; index++)
{
if (entityValue == null)
{
entityValue = list[index];
}
else
{
entityValue += "." + list[index];
}
}

var entityName = GetOptionTitleWithValue("selObjects",entityValue);
if(IsNull(entityName))
{
entityName = RetryEntityName(dataType, entityValue);
}

if (IsNull(attributeName) || IsNull(entityName))
{
if (value.indexOf('#') != -1)
{
entityName = GetLocalizedValueName(value);
}
else
{
return LOCID_DE_NOT_FOUND;
}
}

return attributeName + "(" + entityName + ")";
}
function GetLocalizedValueName(localValueKey)
{
var displayName = null;

try
{
displayName = GetLocalizedLocalValueDisplayName(localValueKey);

}catch(e)
{
}
if(displayName == null)
{
var list = localValueKey.split("#");
displayName = list[2];
}
return displayName;
}
function RetryMatchDataType(dataType, value)
{
var html = GetLatestDataTypeMapping(dataType);
var attributeName = null;
if (!IsNull(html))
{
var control = window.document.getElementById("valueSelectorHidden" + dataType);
if(html.length>1)
{
control.innerHTML = html;
control.DefaultValue = control.DataValue;
attributeName = GetTextFromInnerHtml("<select>" + html + "</select>", value);
}
}

return attributeName;
}
function RetryEntityName(dataType, value)
{
var html = GetEntityListMappingInfo(dataType);
var entityName = null;
if (!IsNull(html))
{
var control = window.document.getElementById("selObjects");
if(html.length>1)
{
control.innerHTML = html;
control.DefaultValue = control.DataValue;
entityName = GetTextFromInnerHtml("<select>" + html + "</select>", value);
}
}
return entityName;
}
function GetTextFromInnerHtml(XmlDoc, value)
{
var oXml = loadXmlDocument(XmlDoc);
var path = "/select/option[@value='" + value +"']";
if(XmlDoc.indexOf("<OPTGROUP")>=0)
{
path = "/select/OPTGROUP/option[@value='" + value +"']";
}

var oNode = oXml.selectSingleNode(path);
if(!IsNull(oNode))
{
return oNode.text;
}

return null;
}
function GetDefaultValueDisplayText(defaultValueNode, dataType)
{
if (dataType == "Integer")
{
var control = window.document.getElementById(defaultValueNode.nodeName);
if (!IsNull(control) && (control.className == "ms-crm-Duration"))
{

return control.FormatDuration(parseInt(defaultValueNode.text,10));
}
else
{
return defaultValueNode.text;
}
}
else if ((dataType == "String") ||
(dataType == "Decimal") ||
(dataType == "Float") ||
(dataType == "Money") ||
(dataType == "DateTime"))
{
return defaultValueNode.text;
}
else if ((dataType == "Picklist") ||
(dataType == "Status") ||
(dataType == "State"))
{
return GetOptionTitleWithValue(defaultValueNode.nodeName,defaultValueNode.text);
}
else if (dataType == "Boolean")
{
var control = window.document.getElementById(defaultValueNode.nodeName);

var inputControls = control.getElementsByTagName("INPUT");
var inputLabels = control.getElementsByTagName("LABEL");

for (var index = 0; index < inputControls.length; index++)
{
if ((inputControls[index].className == "ms-crm-RadioButton") &&
(inputControls[index].value == defaultValueNode.text))
{
return inputLabels[index].innerText;
}
}
}
else if ((dataType == "Lookup") ||
(dataType == "User") ||
(dataType == "Customer") ||
(dataType == "Owner"))
{
return defaultValueNode.attributes[1].nodeValue;
}
else if (dataType == "PartyList")
{
var returnValue = null;
var index = 0;
for(index = 0; index < defaultValueNode.childNodes.length; index++)
{
var childNode = defaultValueNode.childNodes[index];
if (index == 0)
{
returnValue = GetDisplayTextForPartyList(childNode.firstChild);
}
else
{
returnValue += ", " + GetDisplayTextForPartyList(childNode.firstChild);
}
}

return returnValue;
}

return null;
}





function GetDisplayTextForPartyList(node)
{
if(node.nodeName == "addressused")
{
return node.text;
}
else
{
return node.attributes[1].nodeValue;
}
}
function GetOptionTitleWithValue(controlId, value)
{
return GetOptionText(controlId, value, false);
}
function GetOptionValueWithTitle(controlId, title)
{
return GetOptionText(controlId, title, true);
}
function GetOptionText(controlId, compareText, isTitle)
{
var control = window.document.getElementById(controlId);

var index = 0;
for(index=0; index < control.options.length; index++)
{
var option = control[index];
if (isTitle)
{
if (option.title == compareText)
{
return option.desc;
}
}
else
{
if (option.value == compareText)
{
return option.title;
}
}
}

return null;
}
function IsMultiSlug(dataType,control)
{
if (dataType == "String")
{
return true;
}

if (dataType == "PartyList")
{
var imgControls = control.getElementsByTagName("IMG");
for (var index = 0; index < imgControls.length; index++)
{
if ((imgControls[index].className == "ms-crm-Lookup-Party") &&
(imgControls[index].lookupstyle == "single"))
{
return false;
}
}

return true;
}
return false;
}
function IsReadOnly(dataType)
{
if (dataType == "String")
{
return false;
}

return true;
}
function selDblClick()
{
var se = window.event.srcElement;
if(!IsNull(se) && !IsNull(se.value))
{
se.fireEvent("onchange");
}
}
function ShowDurationControl(dataType)
{
var sel = window.document.getElementById("valueSelector");

var durationControl = window.document.getElementById("durationControl");
if (!IsNull(durationControl) && !IsNull(sel))
{
if (dataType == "DateTime" )
{
if(durationControl.style.display != 'block')
{
AttachDurationEvent(true);
durationControl.style.display = 'block';
sel.attachEvent("ondblclick", selDblClick);
}
SetBeforeAfterOperator();
}
else
{
durationControl.style.display = 'none';
sel.detachEvent("ondblclick");
AttachDurationEvent(false);
}
}
}
function AttachDurationEvent(attach)
{
var month = window.document.getElementById("monthSelector");
var day = window.document.getElementById("daySelector");
var hour = window.document.getElementById("hourSelector");
var minute = window.document.getElementById("minuteSelector");
var beforeAfter = window.document.getElementById("beforeAfterSelector");

if(!attach)
{
month.detachEvent("onchange", DurationValueChanged);
day.detachEvent("onchange", DurationValueChanged);
hour.detachEvent("onchange", DurationValueChanged);
minute.detachEvent("onchange", DurationValueChanged);
beforeAfter.detachEvent("onchange", DurationValueChanged);
}
else
{
month.attachEvent("onchange", DurationValueChanged);
day.attachEvent("onchange", DurationValueChanged);
hour.attachEvent("onchange", DurationValueChanged);
minute.attachEvent("onchange", DurationValueChanged);
beforeAfter.attachEvent("onchange", DurationValueChanged);
}
}
function SetBeforeAfterOperator()
{
var beforeAfter = window.document.getElementById("beforeAfterSelector");

if(IsTimeoutCondition(_currentValueControl.ParentControl))
{

if(beforeAfter.options.length ==2)
{
var durationTitle = window.document.getElementById("hidDurationTitle");

var oOption = document.createElement("OPTION");
beforeAfter.options.add(oOption);
oOption.innerText = durationTitle.text;
oOption.value = durationTitle.value;
}
}
else
{
if(beforeAfter.options.length ==3)
{
beforeAfter.options.remove(2);
}
}
}
function ResetDurationValue()
{
var month = window.document.getElementById("monthSelector");
var day = window.document.getElementById("daySelector");
var hour = window.document.getElementById("hourSelector");
var minute = window.document.getElementById("minuteSelector");

month.selectedIndex =0;
day.selectedIndex =0;
hour.selectedIndex =0;
minute.selectedIndex =0;
}
function IsDurationSelected()
{
var month = window.document.getElementById("monthSelector");
var day = window.document.getElementById("daySelector");
var hour = window.document.getElementById("hourSelector");
var minute = window.document.getElementById("minuteSelector");

return month.selectedIndex >0 ||
day.selectedIndex >0 ||
hour.selectedIndex >0 ||
minute.selectedIndex >0 ;
}
function DurationValueChanged()
{
if (IsNull(_currentValueControl))
{
return;
}

var beforeAfter = window.document.getElementById("beforeAfterSelector");
var attributesSelect = window.document.getElementById("valueSelector");
var entityNameSelect = window.document.getElementById('selObjects');
var	entityDisplayArea = window.document.getElementById('divEntitySelectArea');
var variableName;

if(!IsNull(beforeAfter) && beforeAfter.value == DurationOption)
{
if(window.event.srcElement == beforeAfter)
{
var i=0;
for(i=0;i<entityNameSelect.options.length;i++)
{
if(entityNameSelect.options[i].value.indexOf("#Workflow#")== 0)
{
variableName = entityNameSelect.options[i].value;
entityNameSelect.disabled = false;
entityNameSelect.selectedIndex = i;
break;
}
}

entityDisplayArea.style.display = "none";
}

if(IsDurationSelected())
{
var hiddenSelectBox = window.document.getElementById("valueSelectorHiddenDateTime");
var index;
for (index = 0; index < hiddenSelectBox.length; index++)
{
var option = hiddenSelectBox[index];
if (option.value.search(variableName) != -1 && IsTimeoutExpr(option.value))
{
InsertCustomizedDataSlug(option.value);
break;
}
}
}

entityNameSelect.disabled = true;
attributesSelect.disabled = true;
}
else
{
entityDisplayArea.style.display = "block";
entityNameSelect.disabled = false;
attributesSelect.disabled = false;

entityNameSelect.selectedIndex = 0;
}
}
function IsEmailBody(element)
{
return !IsNull(element.className) && element.className.indexOf("ms-crm-Email-Body") >=0;
}
function GetLatestDataTypeMapping(dataType)
{
try
{


return GetHiddenDataTypeMappingInfo(dataType);
}catch(e)
{
}
return null;
}
function IsSlugDeleted(element)
{
while(!IsNull(element) && element.IsSlugDeleted == undefined)
{
element = element.parentElement;
}

if(IsNull(element))
{
return false;
}

return element.IsSlugDeleted;
}
function GetLocalizedLocalValueDisplayName(localValueKey)
{
if(IsNull(localValueKey))
{
return null;
}
var retVal = null;

var command = new RemoteCommand("Workflow", "GetLocalizedLocalValueDisplayName");
command.SetParameter("localValueKey", localValueKey);
var oResult = command.Execute();
if( oResult.Success )
{
retVal = oResult.ReturnValue;
}

if(retVal == "")
{
return null;
}

return retVal;
}
